#!/bin/bash
#date:2013-8-19
#author：mingchao.sun@hc360.com
#控制负载均衡实现灰度发版


LB_PERL=/usr/local/LB_ctrl/vs_pool_V2.0.pl
LB_PERL_DIR=$(dirname $LB_PERL)
LB_TEMP=$LB_PERL_DIR/nodes_status
LB_DEVICE=192.168.70.252

if [ ! -d $LB_TEMP ];then mkdir -pv $LB_TEMP;fi


usage(){
	cat <<help_docs
Usage: sh LB_ctrl.sh --rs 192.168.70.111 --rs 192.168.70.222 -o on
Usage: sh LB_ctrl.sh --rs 192.168.70.111 --rs 192.168.70.222 -o off
help_docs

	exit 1
}
if [ $# -eq 0 ];then usage;fi

#different virtual ip  of cluster with different authority control.
get_auth(){
	case $1 in
		192.168.70.143|192.168.70.144|192.168.70.145|192.168.70.146|192.168.70.147|192.168.70.148)
			USERNAME=test
			PASSWORD=123456
			POOLNAME=pool_bbs
		;;
		192.168.50.115|192.168.50.116|192.168.50.96|192.168.50.75|192.168.50.139)
			USERNAME=test
			PASSWORD=123456
			POOLNAME=pool_vms
		;;
		*)
			echo "Can't obtain username and password of this IP."
			exit 2
		;;
	esac
}

#receive parameters
EAL_SERVERS=()
while [ $# -gt 0 ];do
    case $1 in
       	--rs|--real-server)
			RS=$2
			REAL_SERVERS=(
			${REAL_SERVERS[*]}
			$RS
			)
         	shift
		;;
        -o|--option)
			DO=$2
			shift
        ;;
    	-help|-h|--help)
			usage
         	break
        ;;
      	-*)
			echo $0: $1: unrecognized option. >&2
			usage
         	break
        ;;
       	*)
			usage
         	break
        ;;
    esac
    shift
done

count=${#REAL_SERVERS[*]}

#Usage: wait_signal 90 9 (Sleep 90s with printing '.' split into 9 lines)
wait_signal(){
	for((i=0;i<$1;i++));do
        	m=$(($i % $2))
        	if [ $m -eq 0 ];then
                	echo "";
       		fi
        	echo -n " . ";
        	sleep 1;
	done
	echo ""
}

minus(){
	echo "$1 > $2" | bc
}

worker_stop(){
	cd $LB_PERL_DIR
	real_server=$1
	conn_per=$2
	retry_interval=$3
	retry_times=$4

	#perl $LB_PERL -i $LB_DEVICE -u $USERNAME -p $PASSWORD -P $POOLNAME -S $real_server:80 --enable $DO
	echo "perl $LB_PERL -i $LB_DEVICE -u $USERNAME -p $PASSWORD -P $POOLNAME -S $real_server:80 --enable $DO"
	
	#conn_cur=$(perl $LB_PERL -i $LB_DEVICE -u $USERNAME -p $PASSWORD -P $POOLNAME -S $real_server:80 --status | awk '{print $1}' | awk -F ":" '{print $2}')
	conn_cur=100
	#conn_min=$(expr $conn_per * $conn_cur)	
	conn_min=$(echo $conn_cur| awk '{print $1 * '"$conn_per"' }')
	
	
	stat=0
	#for((i=0;i<$retry_times;i++));do
	j=0
	while (( j < $retry_times ));do
		#conn_cur=$(perl $LB_PERL -i $LB_DEVICE -u $USERNAME -p $PASSWORD -P $POOLNAME -S $real_server:80 --status | awk '{print $1}' | awk -F ":" '{print $2}')
		conn_cur=79
		echo "The count of connections established:$conn_cur ,waiting for being disconnected ."
		wait_signal $retry_interval 3
		loss=$(minus $conn_cur $conn_min)
		
		if [ $loss -ne 1 ];then
			stat=1
			break
		fi
		j=$(expr $j + 1)
	done
	
	if [ $stat -eq 0 ];then
		echo "perl $LB_PERL -i $LB_DEVICE -u $USERNAME -p $PASSWORD -P $POOLNAME -S $real_server:80 --delete"
		echo "ACTION=deleted" > $LB_TEMP/$real_server
	else
		echo "ACTION=disabled" > $LB_TEMP/$real_server
		
	fi

}

worker_start(){
	cd $LB_PERL_DIR
	source $LB_TEMP/$real_server	#get ACTION variable
	real_server=$1
	case $ACTION in
		deleted)
			echo "perl $LB_PERL -i $LB_DEVICE -u $USERNAME -p $PASSWORD -P $POOLNAME -S $real_server:80 --add"
		;;
		disabled)
			echo "perl $LB_PERL -i $LB_DEVICE -u $USERNAME -p $PASSWORD -P $POOLNAME -S $real_server:80 --enable $DO"
		;;
		*)
			echo "Unknown value of variable $ACTION ."
			exit 4
		;;
	esac
}

start_do(){

	case $DO in
		on)
			for((i=0;i<$count;i++));do
				real_server=${REAL_SERVERS[$i]}
				worker_start $real_server
			done
		;;
		off)
			for((i=0;i<$count;i++));do
				real_server=${REAL_SERVERS[$i]}
				worker_stop $real_server 0.7 6 3
			done
		;;
		status)
			for((i=0;i<$count;i++));do
				real_server=${REAL_SERVERS[$i]}
				echo "perl $LB_PERL -i $LB_DEVICE -u $USERNAME -p $PASSWORD -P $POOLNAME -S $real_server:80 --$DO"
			done
		;;
		*)
			echo "OPTION: -o [ on | off | status ]"
			exit 3
		;;
	esac
}

get_auth ${REAL_SERVERS[0]} && start_do